package com.star.carservice.client.adapter;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

import com.squareup.picasso.Picasso;
import com.star.carservice.R;
import com.star.carservice.client.utils.StringUtils;
import com.star.carserviceapi.domain.CarFeatureInfo;

import android.content.Context;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CarBrandListAdapter extends BaseAdapter {

	private static final int ViewType_Group = 0;
	private static final int ViewType_Item = ViewType_Group + 1;
	private static final int ViewType_Max_Count = ViewType_Item + 1;

	private Context mContext = null;
	private List<CarFeatureInfo> mData = null;
	private LayoutInflater mInflater = null;

	public CarBrandListAdapter(Context context, List<CarFeatureInfo> data) {
		super();
		mContext = context;
		mData = data;

		mInflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getItemViewType(int position) {
		return mData.get(position).isIsGroup() ? ViewType_Group : ViewType_Item;
	}

	@Override
	public boolean isEnabled(int position) {

		return !mData.get(position).isIsGroup();
	}

	@Override
	public int getViewTypeCount() {
		return ViewType_Max_Count;
	}

	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;

		int type = getItemViewType(position);
		if (convertView == null) {

			switch (type) {
			case ViewType_Group:

				viewHolder = new ViewHolderGroup();
				convertView = mInflater.inflate(
						R.layout.list_item_car_brand_group, null);

				break;
			case ViewType_Item:

				viewHolder = new ViewHolderItem();
				convertView = mInflater.inflate(
						R.layout.list_item_car_brand_item, null);

				break;
			}

			ButterKnife.inject(viewHolder, convertView);

			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		CarFeatureInfo model = (CarFeatureInfo) getItem(position);

		if (model.isIsGroup()) {
			ViewHolderGroup viewGroup = (ViewHolderGroup) viewHolder;
			viewGroup.lbl_group_name.setText(model.getIndexName());
		} else {
			ViewHolderItem viewItem = (ViewHolderItem) viewHolder;
			viewItem.lbl_brand_name.setText(model.getBrand());		
			
			
			if(!StringUtils.isBlank(model.getThumbUrl())){
				Picasso.with(mContext).load(model.getThumbUrl())
				.into(viewItem.img_brand_thumb);
			}
		}

		return convertView;
	}

	class ViewHolder {

	}

	class ViewHolderGroup extends ViewHolder {
		@InjectView(R.id.lbl_group_name)
		TextView lbl_group_name;
	}

	class ViewHolderItem extends ViewHolder {
		@InjectView(R.id.lbl_brand_name)
		TextView lbl_brand_name;

		@InjectView(R.id.img_brand_thumb)
		ImageView img_brand_thumb;
	}
}
